<template>
  <div class="goods-comment">
    <!-- 测试单选 -->
    <!-- <XtxCheckBox v-model="isImg" /> -->
    <div class="head">
      <div class="data">
        <p>
          <span>{{ goodNums.salesCount }}</span
          ><span>人购买</span>
        </p>
        <p>
          <span>{{ goodNums.praisePercent }}</span
          ><span>好评率</span>
        </p>
        <p>
          <span>{{ goodNums.evaluateCount }}</span
          ><span>条评价</span>
        </p>
        <p>
          <span>{{ goodNums.hasPictureCount }}</span>
          <span>条有图评价</span>
        </p>
      </div>
    </div>
    <!-- 排序 -->
    <div class="sort">
      <span>排序：</span>
      <a
        @click="changeSort(null)"
        href="javascript:;"
        :class="{ active: sortField === null }"
        >默认</a
      >
      <a
        @click="changeSort('praiseCount')"
        href="javascript:;"
        :class="{ active: sortField === 'praiseCount' }"
        >最热</a
      >
      <a
        @click="changeSort('createTime')"
        href="javascript:;"
        :class="{ active: sortField === 'createTime' }"
        >最新</a
      >
      <XtxCheckBox v-model="hasPicture" style="margin-left: 50px"
        >是否有图</XtxCheckBox
      >
    </div>
    <!-- 列表 -->
    <div class="list">
      <div class="item" v-for="item in goodCommentList" :key="item.id">
        <div class="user">
          <img :src="item.member.avatar" alt="" />
          <span>{{ item.member.nickname }}</span>
        </div>
        <div class="body">
          <div class="score">
            <!-- 上边显示得分的实心的五角星 -->
            <i v-for="s in item.score" :key="s" class="iconfont icon-wjx01"></i>
            <!-- <i class="iconfont icon-wjx01"></i>
            <i class="iconfont icon-wjx01"></i>
            <i class="iconfont icon-wjx01"></i> -->
            <!-- 上边显示没分的空心的五角星 -->

            <i
              v-for="n in 5 - item.score"
              :key="n"
              class="iconfont icon-wjx02"
            ></i>
            <span class="attr" v-for="tag in item.tags" :key="tag">{{
              tag
            }}</span>
          </div>
          <div class="text">
            {{ item.content }}
          </div>
          <!-- 展示评论图片 -->
          <GoodCommentImg :pictures="item.pictures" />
          <div class="time">
            <span>{{ item.createTime }}</span>
            <span class="zan"
              ><i class="iconfont icon-dianzan"></i>{{ item.praiseCount }}</span
            >
          </div>
        </div>
      </div>
    </div>
    <!-- 上拉加载更多组件 -->
    <XtxInfiniteLoad
      :isLoading="isLoading"
      :isFinished="isFinished"
      @load="getGoodCommentList"
    />
  </div>
</template>
<script>
import { ref, onMounted, reactive, watch } from 'vue'
import { findCommentInfoByGoods, findCommentListByGoods } from '@/api/goods'
import { useRoute } from 'vue-router'
import { toRefs } from '@vueuse/core'
// 导入评论图片展示组件
import GoodCommentImg from './goods-comment-image.vue'
// 获取商品统计信息
const useGoodNums = () => {
  const goodNums = ref({})
  // 商品ID
  const route = useRoute()
  const getGoodNums = async () => {
    const { result } = await findCommentInfoByGoods(route.params.id)
    console.log(result)
    goodNums.value = result
  }
  onMounted(() => {
    getGoodNums()
  })
  return { goodNums }
}
// 获取当前商品评论列表
const useGoodComment = () => {
  const goodCommentList = ref([])
  // 商品ID
  const route = useRoute()
  // 请求过滤条件
  const reqData = reactive({
    page: 0,
    pageSize: 10,
    hasPicture: false, // 是否有
    sortField: null // 排序: praiseCount -> 最热 createTime -> 最新
  })
  // 实现上拉加载更多
  const isLoading = ref(false)
  const isFinished = ref(false)
  const getGoodCommentList = async () => {
    isLoading.value = true
    reqData.page++
    const { result } = await findCommentListByGoods(route.params.id, reqData)
    // console.log(result)
    // 追加列表数据
    goodCommentList.value.push(...result.items)
    isLoading.value = false
    // 是否加载完成
    if (goodCommentList.value.length === result.counts) {
      isFinished.value = true
    }
  }
  // 条件变化重置列表数据和状态
  const resetList = () => {
    // 重置列表
    // 删除列表数据
    goodCommentList.value = []
    // 重置数据请求状态
    reqData.page = 0
    isLoading.value = false
    isFinished.value = false
  }
  // 点击切换筛选条件
  const changeSort = (currSortField) => {
    reqData.sortField = currSortField
    // 筛选条件变化=》重新请求刷新列表
    // getGoodCommentList()
    resetList()
  }
  // 通过监听是否有图单选框值的变化筛选列表
  watch(() => reqData.hasPicture, () => {
    // 筛选条件变化=》重新请求刷新列表
    // getGoodCommentList()
    resetList()
  })
  // onMounted(() => {
  //   getGoodCommentList()
  // })
  return {
    goodCommentList,
    getGoodCommentList,
    reqData,
    changeSort,
    isLoading,
    isFinished
  }
}
export default {
  name: 'GoodsComment',
  components: {
    GoodCommentImg
  },
  setup () {
    const { goodNums } = useGoodNums()
    const { goodCommentList, changeSort, getGoodCommentList, isLoading, isFinished, reqData } = useGoodComment()
    return { goodNums, goodCommentList, getGoodCommentList, isLoading, isFinished, changeSort, ...toRefs(reqData) }
  }
}
</script>
<style scoped lang="less">
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;
    .data {
      flex: 1;
      display: flex;
      padding: 20px;
      p {
        flex: 1;
        text-align: center;
        span {
          display: block;
          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }
          &:last-child {
            color: #999;
          }
        }
      }
    }
  }
  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;
    > span {
      margin-left: 20px;
    }
    > a {
      margin-left: 30px;
      &.active,
      &:hover {
        color: @xtxColor;
      }
    }
  }
}
.list {
  padding: 0 20px;
  .item {
    display: flex;
    padding: 25px 10px;
    border-bottom: 1px solid #f5f5f5;
    .user {
      width: 160px;
      img {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        overflow: hidden;
      }
      span {
        padding-left: 10px;
        color: #666;
      }
    }
    .body {
      flex: 1;
      .score {
        line-height: 40px;
        .iconfont {
          color: #ff9240;
          padding-right: 3px;
        }
        .attr {
          padding-left: 10px;
          color: #666;
        }
      }
    }
    .text {
      color: #666;
      line-height: 24px;
    }
    .time {
      color: #999;
      display: flex;
      justify-content: space-between;
      margin-top: 5px;
    }
  }
}
</style>
